//
//  ViewController.m
//  14-CollectionViewTest
//
//  Created by Dendi on 8/2/16.
//  Copyright © 2016 Kaleidoscope. All rights reserved.
//

#import "ViewController.h"
#import "imageCell.h"
#import "LabelView.h"
#import "LabelAndCollectionViewCollectionReusableView.h"
#define kArcRandomColor [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0]
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0]

#define kCollectionVIewReusableViewHeaderNoCell @"header"


@interface ViewController ()
<

    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout
>

@end

@implementation ViewController

static NSString * const ID = @"cell";
static NSString * const ID1 = @"cell1";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    
    flow.headerReferenceSize = CGSizeMake(0, 50);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    CGFloat itemWidth = (CGRectGetWidth(self.view.bounds) - 10) / 3;
    flow.itemSize = CGSizeMake(itemWidth, itemWidth);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 5;
    [self.view addSubview:collectionView];
    collectionView.backgroundColor = [UIColor lightGrayColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[imageCell class] forCellWithReuseIdentifier:ID];
    [collectionView registerClass:[LabelView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ID1];
    
    // 方案一: 通过实现上边距实现视图添加(UICollectionView本质是UIScorllView)
//    collectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -295, self.view.bounds.size.width, 290)];
//    view.backgroundColor = [UIColor greenColor];
//    [collectionView addSubview:view];
    
    [collectionView registerClass:[LabelAndCollectionViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionVIewReusableViewHeaderNoCell];
    
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
    } else {
        return CGSizeMake(0, 50);
        
    }
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 2;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        LabelAndCollectionViewCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionVIewReusableViewHeaderNoCell forIndexPath:indexPath];
        return view;
        
        
    }
    

    LabelView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ID1 forIndexPath:indexPath];
//    reusableView.backgroundColor = kArcRandomColor;
    return reusableView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {

    return 0;
    
    }
    return 100;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.contentView.backgroundColor = kArcRandomColor;
    return cell;
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}


@end
